package cn.yuhe.org.controller;

import cn.yuhe.basic.annotation.PreAuthorize;
import cn.yuhe.basic.utils.AjaxResult;
import cn.yuhe.basic.utils.PageList;
import cn.yuhe.org.domain.Department;
import cn.yuhe.org.query.DepartmentQuery;
import cn.yuhe.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

/**
* @Description: 控制层
* @Author: Mr.Qiu
* @Version: 1.0
* @Date:
*/
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /** 
    * @Title: 根据id查询
    * @Description: 根据id查询
    * @Author: Mr.Qiu
    * @Version: 1.0
    * @Date:  2022/6/24 10:13  
    * @Parameters: [id]
    * @Return cn.yuhe.org.domain.Department
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    @PreAuthorize(name = "查询单个部门",sn = "department:loadById")
    public Department loadById(@PathVariable("id") Long id){

        return departmentService.loadById(id);
    }

    /**
    * @Description: 查询所有
    * @Author: Mr.Qiu
    * @Version: 1.0
    * @Date:
    */
    @GetMapping
    @PreAuthorize(name = "查询全部部门",sn = "department:loadAll")
    public List<Department> loadAll(){

        return departmentService.loadAll();
    }

    /** 
    * @Title: 根据id删除
    * @Description: 根据id删除
    * @Author: Mr.Qiu
    * @Version: 1.0
    * @Date:  2022/6/24 11:53  
    * @Parameters: []
    * @Return java.util.List<cn.yuhe.org.domain.Department>
    */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除部门",sn = "department:remove")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
           departmentService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    /**
    * @Title: 新增修改
    * @Description: 新增修改
    * @Author: Mr.Qiu
    * @Version: 1.0
    * @Date:  2022/6/24 12:56
    * @Parameters: [department]
    * @Return cn.yuhe.basic.utils.AjaxResult
    */
    @PutMapping
    @PreAuthorize(name = "新增修改部门",sn = "department:addOrUpt")
    public AjaxResult addOrUpt(@RequestBody Department department){
        try {
            if (department.getId() == null) {
                 departmentService.save(department);
            } else {
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }


    @PostMapping
    @PreAuthorize(name = "分页&高级查询部门",sn = "department:querData")
    public PageList<Department> querData(@RequestBody DepartmentQuery departmentQuery){
            PageList<Department> data = departmentService.queryData(departmentQuery);
            return data;
    }

    @PatchMapping
    @PreAuthorize(name = "批量删除",sn = "department:batchDel")
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            departmentService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }
    /**
    * @Title: tree
    * @Description: 查询部门树数据
    * @Author: Mr.Qiu
    * @Version: 1.0
    * @Date:  2022/6/26 10:00
    * @Parameters: []
    * @Return java.util.List<cn.yuhe.org.domain.Department>
    */
    @GetMapping("/tree")// 请求地址：http://localhost:8080/department GET请求
    public List<Department> tree(){
        return departmentService.tree();
    }

}
